package cn.tedu.search.controller;


import cn.tedu.search.service.SearchService;
import cn.tedu.search.service.SearchServiceImpl;
import com.easyhouse.common.pojo.Product;
import com.easyhouse.common.utils.MapperUtil;
import com.easyhouse.common.utils.StringConversionJavabeanUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/search/manage")
public class SearchController {
    @Autowired(required = false)
    private SearchService searchService;
    /*
        easymall 首页搜索数据 query 是个文本
        page rows分页数据
     */
    @RequestMapping("/query")
    public List<Product> search(
            @RequestParam("query") String text,
            @RequestParam(name = "page",defaultValue = "1") Integer page,
            @RequestParam(name = "rows",defaultValue = "5") Integer rows){
        return searchService.search(text,page,rows);
    }

    @RequestMapping(value = "/insert/index",method = RequestMethod.POST)
    public void addingIndex(@RequestParam("productString") String productString){
        //添加索引数据
        searchService.addingIndex(productString);
    }

    //修改索引数据
    @RequestMapping(value = "/update/index",method = RequestMethod.POST)
    public void updateIndex(@RequestParam("productString") String productString){
            //把string转换成Javabean
            Product product = StringConversionJavabeanUtils.StringJava(productString);
            //根据houseId修改索引
            searchService.updateIndex(product);
            //添加索引数据
            searchService.addingIndex(productString);
    }

    //删除索引
    @RequestMapping(value = "/delete/index",method = RequestMethod.POST)
    public void deleteIndex(){
        searchService.deleteIndex();
    }

    //购买成功删除索引数据
    @RequestMapping(value = "/delete/indexData",method = RequestMethod.POST)
    public void deleteIndexData(@RequestParam("houseId") String houseId){
        searchService.deleteIndexData(houseId);
    }
}
